package com.gengine.core.frame
{
	import com.gengine.global.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class FlashFrame extends Frame
	{
		private var _sprite:Sprite;
		private var _isBrowserAvailable:Boolean = false;
		private var _count:int = 0;
		private var _accruedTime:Number;
		private var _startTime:Number;
		public static var AverageTime:int = 33;
		public static var FramesCount:int = 0;
		
		public function FlashFrame(delay:Number = 24)
		{
			super(delay);
			this._sprite = new Sprite();
		}
		
		override public function play() : void
		{
			super.play();
			this._sprite.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
			this._count = 0;
			this._startTime = getTimer();
		}
		
		override public function stop() : void
		{
			super.stop();
			this._sprite.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
		}
		
		private function onEnterFrame(event:Event = null) : void
		{
			if (this._isBrowserAvailable == false)
			{
				this.onEnterFrameHandler(event);
			}
			else if (Global.isActivate)
			{
				this.onEnterFrameHandler(event);
			}
		}
		
		override protected function onEnterFrameHandler(event:Event = null) : void
		{
			FramesCount = 0;
			super.onEnterFrameHandler(event);
			_count = _count + 1;
			if (this._count == 50)
			{
				AverageTime = (getTimer() - this._startTime) / this._count;
				this._count = 0;
				this._startTime = getTimer();
			}
		}
		
		override public function dispatchEvent() : void
		{
			this._isBrowserAvailable = true;
			if (Global.isActivate == false)
			{
				this.onEnterFrameHandler();
			}
		}
	}
}
